<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 列表管理
 */
namespace app\api\model;

use app\api\model\Model;

class Article extends Model{
	
    private $article = 'sys_article';
    private $articleclass = 'sys_articleclass';

    /**
     * 類別列表
     * @param array $catelist 
     * @param array $where 
     */ 
    public function cate($where=null, $page=1, $page_size=10, $field='a.*'){
        //類別列表
        $catelist['data']= Model::name($this->articleclass)
            ->alias('a')
            ->field($field)
            ->where($where)
            ->order('a.id desc')
            ->limit(($page-1)*$page_size,$page_size)
            ->select()
            ->toArray();
        //数据总数
        $catelist['total']=Model::name($this->articleclass)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $catelist; 
    }

    /**
     * 文章列表
     * @param array $select 
     * @param array $where 
     */ 
    public function select($where=null, $page=1, $page_size=10, $field='a.*'){
        //矿机列表
        $list['data']= Model::name($this->article)
            ->alias('a')
            ->join($this->articleclass.' b','a.cid=b.id','left')
            ->field($field.', b.title as cate, b.acode as code')
            ->order('a.id desc')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->select()
            ->toArray();
        if(!empty($list['data'])){
            foreach ($list['data'] as $key => $value) {
                $list['data'][$key]['imgurl']=config('file.baseurl').$value['imgurl'];
            }
        }else{
            $list['data']=[];
        }
        
        //数据总数
        $list['total']=Model::name($this->article)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;
    }

}
